package com.wwwarehouse.resource_center.fragment.resource_efficiency_evaluation;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.fragment.CommonBridgeWebViewFragment;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.WebDataBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;

/**
 * @author Joseph on 2017/11/1.
 *         <p/>
 *         趋势图webView
 */

public class TrendChartWebViewFragment extends CommonBridgeWebViewFragment {
    private String mBaseUrl = "";

    @Override
    public void requestDatas() {
        super.requestDatas();
        Log.d("getBusinessParam", mData);
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));
        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = "";
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_RELEASE;
        }

        String newUrl = mBaseUrl + ResourceConstant.TREND_ECHART;
        mWebView.loadUrl(newUrl);
        mWebView.callHandler("getBusinessParam", mData, new CallBackFunction() {
            @Override
            public void onCallBack(String data) {
                ToastUtils.showToast(data);
            }
        });

        mWebView.registerHandler("didClickFullScreenButtonAction", new BridgeHandler() {
            public void handler(String data, CallBackFunction function) {
                WebDataBean dataBean = JSON.parseObject(data, WebDataBean.class);
                WebDataBean dataBean1 = JSON.parseObject(mData, WebDataBean.class);
                dataBean1.setIndex(dataBean.getIndex());
                dataBean1.setUrl(dataBean.getUrl());
                Intent intent = new Intent(getActivity(), ResourceWebViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("data", dataBean1);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof TrendChartWebViewFragment)
            mActivity.setTitle(getString(R.string.trend_chart_webview));
    }

    @Override
    public void onEventMainThread(BackListenerEvent event) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//            super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
//                    String index = data.getStringExtra("data");
                Log.d("onActivityResult", "index");
                break;
        }
    }
}
